package com.googlecode.antilopebpm.cfg;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.antilopebpm.util.ConfigHelper;

public class AntilopeConfiguration {
	private static final Logger logger = LoggerFactory
			.getLogger(AntilopeConfiguration.class);

	public static final String VARIABLE_BYTE_BLOCK_SIZE = "variable.byteArray.block.size";
	public static final String PROCDEFINITION_BYTEARRAY_BLOCK_SIZE = "procDefinition.byteArray.block.size";

	private static final Properties PROPERTIES = new Properties();

	static {
		logger.info("Antilope Version:" + Version.VERSION);
		InputStream defaultStream = null;
		InputStream stream = null;

		try {
			defaultStream = ConfigHelper
					.getResourceAsStream("/antilope.default.properties");
			PROPERTIES.load(defaultStream);
			defaultStream = ConfigHelper
					.getResourceAsStream("/antilope.properties");
			Properties tempProperties = new Properties();
			tempProperties.load(defaultStream);

			PROPERTIES.putAll(tempProperties);
			logger.info("loaded properties from resource abpm.properties");
		} catch (Exception e) {
			logger
					.warn("could not loading properties from abpm's properties:caused by["
							+ e.getMessage() + "]");
		} finally {
			try {
				if (defaultStream != null) {
					defaultStream.close();
				}
				if (stream != null) {
					stream.close();
				}
			} catch (IOException ioe) {
				logger.error("could not close stream on abpm.properties", ioe);
			}
		}

		try {
			PROPERTIES.putAll(System.getProperties());
		} catch (SecurityException e) {
			logger
					.warn("could not copy system properties, system properties will be ignored:caused by:\n"
							+ e + "]");
		}

	}

	public static boolean getBoolean(String property) {
		String setting = PROPERTIES.getProperty(property);
		return setting != null
				&& Boolean.valueOf(setting.trim()).booleanValue();
	}

	public static int getInt(String property) {
		String propValue = PROPERTIES.getProperty(property);
		return Integer.parseInt(propValue.trim());
	}

	public static String getString(String property, String defaultValue) {
		String propValue = PROPERTIES.getProperty(property);
		return propValue == null ? defaultValue : propValue;
	}

}
